import 'package:flutter/material.dart';
import 'package:flutter_getx/utils/token.dart';
import 'package:get/get_navigation/src/routes/route_middleware.dart';

class AuthMiddleware extends GetMiddleware {
  static final tokenManager = TokenManager();
  @override
  RouteSettings? redirect(String? route) {
    String token = tokenManager.getToken() ?? '';
    bool isLoggedIn = token.isNotEmpty;

    // 已登录，访问登录页，跳转到首页
    if (isLoggedIn && route == '/login') {
      return const RouteSettings(name: '/home');
    }

    // 未登录，访问 /profile 时跳转到 /login
    if (!isLoggedIn && route == '/profile') {
      return const RouteSettings(name: '/login');
    }

    // 其它情况允许通过
    return null;
  }
}
